package com.treeliked.netdiskdemo.controller;

import com.treeliked.netdiskdemo.model.Response;
import com.treeliked.netdiskdemo.model.ResultCode;
import com.treeliked.netdiskdemo.model.dataobject.File;
import com.treeliked.netdiskdemo.service.FileService;
import com.treeliked.netdiskdemo.util.FileUtils;
import com.treeliked.netdiskdemo.util.OssUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * file api mapper
 *
 * @author lqs2
 * @date 2018/12/5, Wed
 */
@Controller
@RequestMapping("/api/file")
@Transactional(rollbackFor = RuntimeException.class)
public class FileController {


    private static Logger log = LoggerFactory.getLogger(FileController.class);

    private final FileService fileService;

    @Autowired
    public FileController(FileService fileService) {
        this.fileService = fileService;
    }

    @PostMapping(value = "/upload-file", consumes = "multipart/form-data")
    public @ResponseBody
    Response uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam("path") String path,
            @RequestParam("author") String author) {
        Response resp = new Response();
        if (!file.isEmpty()) {
            try {
                fileService.saveFile(file, path, author, resp);
            } catch (IOException e) {
                log.error("", e);
                resp.setCode(ResultCode.INTERNAL_SERVER_ERROR);
                resp.setMessage("上传失败," + e.getMessage());
            }
        } else {
            resp.setCode(ResultCode.FAIL);
            resp.setMessage("上传失败，文件为空");
        }
        return resp;
    }


    @GetMapping(value = "/download")
    public void downloadFile(@RequestParam("id") String fileId, HttpServletResponse response) {
        try {
            com.treeliked.netdiskdemo.model.dataobject.File file = fileService.getFileById(fileId);

            if (file != null) {
                String ossKey = file.getId() + "." + file.getSuffix();

                // 获取oss 文件输入流
                InputStream objectStream = OssUtils.getObjectStream(ossKey);

                if (objectStream != null) {
                    // 设置响应头
                    FileUtils.setFileDownloadHeader(response, file.getName());
                    try {
                        fileService.downloadFile(objectStream, response.getOutputStream());
                    } catch (Exception e) {
                        log.error("", e);
                    }
                }
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }

    @GetMapping(value = "/rm")
    public @ResponseBody
    Response removeFile(String id) {
        Response resp = new Response();
        try {
            File file = fileService.getFileById(id);
            fileService.deleteFileById(id);
            resp.setCode(ResultCode.SUCCESS);
            resp.setMessage("文件【" + file.getName() + "】删除成功");
        } catch (Exception e) {
            log.error("", e);
            resp.setCode(ResultCode.INTERNAL_SERVER_ERROR);
            resp.setMessage("文件删除失败");
        }
        return resp;
    }
}
